﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Threading;
using LocalService;
using Models;
using OnTheWay.Exceptions;
using OnTheWay.Tool;

namespace PcBackgroundMgr
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            LightCache cache = new LightCache();
            LoginName = cache.Get<String>("LoginName", "");
            IsRemember = cache.Get<bool>("IsRemember", false);
            if (IsRemember)
            {
                Password = cache.Get<String>("Password", "");
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void mbtQuit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            try
            {Process.GetCurrentProcess().Kill();}
            catch
            { }
        }



        public bool IsRemember
        {
            get { return (bool)GetValue(IsRememberProperty); }
            set { SetValue(IsRememberProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsRemember.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsRememberProperty =
            DependencyProperty.Register("IsRemember", typeof(bool),
            typeof(LoginWindow), new UIPropertyMetadata(false));


        public string LoginName
        {
            get { return (string)GetValue(LoginNameProperty); }
            set { SetValue(LoginNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LoginName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LoginNameProperty =
            DependencyProperty.Register("LoginName", typeof(string), 
            typeof(LoginWindow), new UIPropertyMetadata(""));



        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string),
            typeof(LoginWindow), new UIPropertyMetadata(""));

        

        private void mbtLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(LoginName) || string.IsNullOrEmpty(Password))
            {
                MessageBox.Show(this, "请先输入完整信息！");
                return;
            }
            bool isRemember = IsRemember;
            string loginname = LoginName;
            string oldpassword = Password;
            string password = BlackMofoApp.App.EncryptionPassword(oldpassword);
            LoginWaiting waiting = new LoginWaiting();
            waiting.Show();
            this.Hide();
            new Thread(() =>
            {
                try
                {
                    SysAdmin admin = LocalFactory.SysAdminLocal.Verify(loginname, password);
                    if (admin == null)
                    {
                        throw new Exception("用户名或者密码错误！");
                    }
                    LightCache cache = new LightCache();
                    cache.Put("LoginName", loginname);
                    cache.Put("Password", oldpassword);
                    cache.Put("IsRemember", isRemember);
                    Dispatcher.Invoke(new Action(() =>
                    {
                        waiting.Close();
                        BlackMofoApp.App.Admin = admin;
                        Window main = new MainWindow();
                        main.Show();
                        this.Close();
                    }));
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(new Action(() =>
                    {
                        waiting.Close();
                        this.Show();
                        MessageBox.Show(this,ExceptionHandle.GetMessage(ex), "登录失败");
                    }));
                }
            }).Start();
        }

        private void mbtLogin_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            LoginWaiting waiting = new LoginWaiting();
            waiting.Show();
            this.Hide();
            bool isRemember = IsRemember;
            string loginname = "admin";
            string oldpassword = "123456";
            string password = BlackMofoApp.App.EncryptionPassword(oldpassword);
            new Thread(() =>
            {
                SysAdmin admin = null;
                try
                {
                    LightCache cache = new LightCache();
                    cache.Put("LoginName", loginname);
                    cache.Put("Password", oldpassword);
                    cache.Put("IsRemember", isRemember);
                    admin = LocalFactory.SysAdminLocal.Verify(loginname, password);
                }
                catch (Exception)
                {
                } 
                if (admin == null)
                {
                    admin = new SysAdmin("开发者");
                }
                Dispatcher.Invoke(new Action(() =>
                {
                    waiting.Close();
                    BlackMofoApp.App.Admin = admin;
                    Window main = new MainWindow();
                    main.Show();
                    this.Close();
                }));
            }).Start();
        }
    }
}
